package com.hundsun.fcp.activity;

import java.util.regex.Pattern;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.hundsun.fcp.R;
import com.hundsun.fcp.base.BaseActivity;
import com.hundsun.fcp.common.FcpConfig;
import com.hundsun.fcp.common.TradeRequest;
import com.hundsun.fcp.common.AppointFiled.IntentKeyDef;
import com.hundsun.fcp.common.utils.StringUtils;
import com.hundsun.fcp.common.utils.ViewUtils;
import com.hundsun.fcp.manager.FcpUserManager;
import com.hundsun.fcp.openapi.trade.bean.RequestUserItemBean;
import com.hundsun.fcp.widget.EditTextWithClear;

public class MineModifyNicknameActivity extends BaseActivity {
	private EditTextWithClear mNickname;
	private ImageView mSave;
	private String mNewText;
	private RequestUserItemBean mBean ;
	private String mOldName;
	protected int getContentLayout() {
		return R.layout.fcp_mine_set_nickname;
	}
	@Override
	protected void initHeader() {
		super.initHeader();
		mHeaderMenu.setTitle(R.string.mine_accinfo_nickname);
		mHeaderMenu.setRightText(R.string.mine_head_save) ;	
		mHeaderMenu.setRightClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				try{
					String errInfo = vfyEidtIpt();
					
					if(StringUtils.isEmpty(errInfo)){
						if(mNewText.equals(mOldName)){
							finish() ;
							return ;
						}
						
						Pattern pattern = Pattern.compile("[\u4e00-\u9fa5a-zA-Z0-9_]+") ;
						if(!pattern.matcher(mNewText).matches()){
							mNickname.setError("昵称只能使用中文、字母、数字和下划线") ;
							return ;
						}
						mBean.clientno = FcpUserManager.getInstance().getUserClientNo() ;
						mBean.shortname = mNewText ;
						TradeRequest.doUpdateUserItem(MineModifyNicknameActivity.this, mBean, true) ;
						
					}else{
						mNickname.setError(errInfo) ;
					}
				}catch(Exception e){
					if(FcpConfig.debugon){
						e.printStackTrace() ;
					}
				}
			}
		});
	}
	@Override
	public void initView() {
		super.initView();
		mNickname = (EditTextWithClear) findViewById(R.id.et_username);
		mSave = (ImageView) findViewById(R.drawable.btn_save);
		mOldName = getIntent().getStringExtra(IntentKeyDef.USERITEM_OLD) ;
		mNickname.setText(mOldName) ;
		mBean = new RequestUserItemBean() ; 
	}
	
	private String vfyEidtIpt(){
		String errStr = "";
		mNewText = ViewUtils.getText(mNickname);
		if(StringUtils.isEmpty(mNewText)){
			errStr = "请输入"+mHeaderMenu.getTitle();
			return errStr;
		}
		
		
		return errStr ;
	}
}
